﻿using System;
using System.Configuration;
using Subgurim.Controles;


public partial class FindLocation : System.Web.UI.Page
{
    protected Subgurim.Controles.GMarker oMarker;


    protected void btnLocate_Click(System.Object sender, System.EventArgs e)
    {
        GeoCode GeoCode;
        string sMapKey = ConfigurationManager.AppSettings["googlemaps.subgurim.net"];

        GeoCode = GMap.geoCodeRequest(txtLocation.Text, sMapKey);
        if (GeoCode.valid == true)
        {
            txtLat.Text = GeoCode.Placemark.coordinates.lat.ToString();
            txtLong.Text = GeoCode.Placemark.coordinates.lng.ToString();
            var gLatLng = new Subgurim.Controles.GLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng);

            GMap1.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
            var oMarker = new Subgurim.Controles.GMarker(gLatLng);
            AddMarker(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng);

        }
        else
        {
            Msg("Không thể tìm thấy địa chỉ này!.");
        }

    }

    protected void btnCustomLocate_Click(System.Object sender, System.EventArgs e)
    {
        if (txtLat.Text != "")
        {
            var gLatLng = new Subgurim.Controles.GLatLng(Convert.ToDouble(txtLat.Text), Convert.ToDouble(txtLong.Text));

            GMap1.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
            var oMarker = new Subgurim.Controles.GMarker(gLatLng);
            AddMarker(Convert.ToDouble(txtLat.Text), Convert.ToDouble(txtLong.Text));

        }
        else
        {
            Msg("Không thể tìm thấy địa chỉ này!.");
        }

    }
    private void AddMarker(double dLat, double dLong)
    {
        Subgurim.Controles.GLatLng gLatLng = new Subgurim.Controles.GLatLng(dLat, dLong);

        Subgurim.Controles.GMarkerOptions oOption = new Subgurim.Controles.GMarkerOptions();
        oOption.draggable = true;

        GMap1.resetMarkers();
        GMap1.resetListeners();

        oMarker = new Subgurim.Controles.GMarker(gLatLng, oOption);

        GMap1.addGMarker(oMarker);
        GMap1.addListener(new Subgurim.Controles.GListener(oMarker.ID, Subgurim.Controles.GListener.Event.dragend, "function(){ document.getElementById('hidLat').value=" + oMarker.ID + ".getPoint().lat();document.getElementById('hidLng').value=" + oMarker.ID + ".getPoint().lng() }"));

    }
    protected void btnSave_Click(System.Object sender, System.EventArgs e)
    {
        txtLat.Text = hidLat.Value;
        txtLong.Text = hidLng.Value;
        GMap1.resetMarkers();
        GMap1.resetListeners();
        AddMarker(Convert.ToDouble(hidLat.Value), Convert.ToDouble(hidLng.Value));
    }

    private void Msg(string sText)
    {
        //Page.ClientScript.RegisterClientScriptBlock(GetType(String), "msg", "<script>Alert('" & sText & "');</script>")

        Response.Write("<script type='text/javascript'>alert('" + sText + "');</script>");

    }

}